package cn.jdy.plugins.admin;

import java.net.MalformedURLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.constant.GlobalConstant;
import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.plugins.entity.Website;
import cn.jdy.plugins.service.WebsiteService;

/**
 * 网站基本信息管理
 *
 * 
 */
@Controller
@RequestMapping("/website")
public class WebsiteCtrl extends BaseCtrl {
	@Autowired
	private WebsiteService websiteService;

	
	@RequestMapping("/index")
	public ModelAndView add() {
		
	/*List<Website> listwebsite=websiteService.findAll();
		if(listwebsite!=null && listwebsite.size()>0){
			return edit(listwebsite.get(0).getId());
		}else{
			return new ModelAndView("/website/website.jsp");
		}*/
		
		Website ws=new Website();
		ws.setUrl(returnUrls(request));
		List<Website> listwebsite=websiteService.query(ws);
		if(listwebsite!=null && listwebsite.size()>0){
			return edit(listwebsite.get(0).getId());
		}else{
			return new ModelAndView("/website/website.jsp");
		}
	}
	
	/**
	 *  初始化
	 * @param entity
	 * @param fromDate
	 * @param toDate
	 * @param s
	 * @return
	 */
	@RequestMapping("/list")
	public ModelAndView list(Website entity, String fromDate, String toDate, @RequestParam(required = false) Character s) {
		ModelAndView mv = new ModelAndView("/mall/mallList.jsp");
		if(s != null)
			switch(s) {
				case '1': mv.addObject("msg", "保存成功！");break;
				case '2': mv.addObject("msg", "更新成功！");break;
				case '3': mv.addObject("msg", "删除成功！");break;
				case '4': mv.addObject("msg", "操作失败！");break;
				default: break;
			}
    mv.addObject("classList", confService.queryByType(GlobalConstant.TYPE_PRODUCTX, 0L));
		mv.addObject("entity", entity);
		return mv;
	}
	
	/**
	 * 异步查询
	 * @param sp
	 * @param fromDate
	 * @param toDate
	 * @return
	 */
	@RequestMapping("/queryByPage")
	@ResponseBody
	public Map<String, Object> queryByPage(Website sp){
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			jsonMap.put("dataList", websiteService.query(sp));
			jsonMap.put("iTotalDisplayRecords", websiteService.count(sp));
			jsonMap.put("iTotalRecords", websiteService.count());
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询异常", e);
		}
		return jsonMap;
	}
	
	@RequestMapping("/edit/{id}")
	public ModelAndView edit(@PathVariable String id) {
		ModelAndView mv = new ModelAndView();
		Website website = this.websiteService.find(id);
		if(website != null) {
			mv.setViewName("/website/website.jsp");
			mv.addObject("data", website);
		}
		return mv;
	}

	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(Website entity) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			if(entity.getId() == null || entity.getId().length()==0) {
				entity.setId(null);
				this.websiteService.save(entity);
			}else{
			  this.websiteService.updateWithValidator(entity);
			}
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(getClass(), "网站基本信息更新异常！", e);
		}
		return jsonMap;
	}
	public  static String returnUrls(HttpServletRequest request) {
		 String urls = request.getRequestURL().toString();
				
		 java.net.URL url = null;
		try {
			url = new  java.net.URL(urls);
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
				
		return url.getHost();
		
	}

}